package minTrioDegree;

public class Solution {
    public int minTrioDegree(int n, int[][] edges) {
        int[] deg = new int[n+1];
        int[][] mask = new int[n+1][n+1]; // 表示点i j 之间有一条边
        for (int[] edge: edges) {
         deg[edge[0]]++;
         deg[edge[1]]++;
         mask[edge[0]][edge[1]] = 1;
         mask[edge[1]][edge[0]] = 1;
        }
        int ans = -1;
        for (int i = 1; i <=n ; i++) {
            for (int j = i+1; j <= n; j++) {
                if(mask[i][j] == 0) continue; // i和j之间没有边
                for (int k = j+1; k <= n; k++) {
                    // 取三个点 判断是否是联通三元组
                    if(mask[j][k] == 0) continue; // j 和 k 之间没有边
                    if(mask[i][k] == 0) continue; // i 和 k 之间没有边
                    int t = deg[i] + deg[j] + deg[k] - 6; // 三个点的度之和 - 6
                    if(ans == -1)  ans = t;
                    else ans = Math.min(ans,t);
                }
            }
        }
        return ans;
    }
}
